package com.origin.oss.entity;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.origin.oss.enums.FileStateEnum;
import com.origin.oss.enums.StorageType;
import lombok.Data;
import org.springframework.data.annotation.Id;

import java.time.LocalDateTime;

/**
 * 文件信息
 *
 * @author origin
 * @date 2024/2/5 13:24
 */
@TableName("file_info")
@Data
public class FileInfo {

    @Id
    @TableId(value = "file_id", type = IdType.INPUT)
    private String fileId;

    /**
     * md5 唯一标识
     */
    @TableField("identifier")
    private String identifier;

    /**
     * 文件 url
     */
    @TableField("file_url")
    private String fileUrl;

    /**
     * 文件大小
     */
    @TableField("file_size")
    private Long fileSize;

    /**
     * 文件状态
     */
    @TableField("file_state")
    private Integer fileState;

    /**
     * 存储类型
     */
    @TableField("storage_type")
    private Integer storageType;

    /**
     * 创建时间
     */
    @TableField("create_datetime")
    private LocalDateTime createDateTime;


    public static FileInfo init(String identifier, String fileUrl, long fileSize, StorageType storageType) {
        FileInfo fileInfo = new FileInfo();
        fileInfo.fileId = IdUtil.getSnowflakeNextIdStr();
        fileInfo.identifier = identifier;
        fileInfo.fileState = FileStateEnum.UPLOADING.code;
        fileInfo.fileUrl = fileUrl;
        fileInfo.fileSize = fileSize;
        fileInfo.storageType = storageType.getCode();
        fileInfo.createDateTime = LocalDateTime.now();
        return fileInfo;
    }

}
